import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/assessment/assessmentRecordsResult.dart';
import 'package:ichinesereader/models/assessment/assessmentTeacherQueryDto.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/assessmentsRecordsDataGrid.dart';
import '../../themes/appTheme.dart';

class TeacherAssessmentRecords extends StatefulWidget {
  TeacherAssessmentRecords({Key? key}) : super(key: key);
  static const String routeName = '/teacher/reports/assessment';

  @override
  _TeacherAssessmentRecords createState() => _TeacherAssessmentRecords();
}

class _TeacherAssessmentRecords extends State<TeacherAssessmentRecords> {
  final ReportsController _reportsController = Get.put(ReportsController());
  var box = Hive.box('appconfig');
  List<String> status = [];

  @override
  void initState() {
    var classId = box.get(CacheManagerKey['currentclass'].toString());
    if (_reportsController.teacherAssessmentRecord.value.list != null &&
        _reportsController.teacherAssessmentRecord.value.list!.isNotEmpty) {
      _reportsController.teacherAssessmentRecord.value.list!.clear();
    }
    Future.delayed(Duration(milliseconds: 200), () {
      AssessmentTeacherQueryDto dto =
          new AssessmentTeacherQueryDto(status: [], type: []);
      _reportsController.fetchTeacherAssessmentRecords(classId, dto);
    });
    super.initState();
  }

  void fetchData(classId, status) {
    AssessmentTeacherQueryDto dto =
        new AssessmentTeacherQueryDto(status: status, type: []);
    _reportsController.fetchTeacherAssessmentRecords(classId, dto);
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher report Assessment');
    var classId = box.get(CacheManagerKey['currentclass'].toString());

    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Column(children: [
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 9.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                ChoiceChip(
                  label: Text(
                    "New",
                    style: TextStyle(color: AppTheme.colors.lightOrange),
                  ),
                  onSelected: (value) {
                    List<String> temp = status;
                    if (!value)
                      temp.remove("SUBMITTED");
                    else
                      temp.add("SUBMITTED");
                    setState(() {
                      status = temp;
                    });
                    fetchData(classId, temp);
                  },
                  selected: status.contains("SUBMITTED"),
                  elevation: 1,
                  backgroundColor: Colors.grey,
                  selectedColor: AppTheme.colors.orangeBtn,
                  labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 6.0),
                  child: ChoiceChip(
                    label: Text(
                      "Processing",
                      style: TextStyle(color: AppTheme.colors.lightOrange),
                    ),
                    onSelected: (value) {
                      List<String> temp = status;
                      if (!value)
                        temp.remove("PROCESSING");
                      else
                        temp.add("PROCESSING");
                      setState(() {
                        status = temp;
                      });
                      fetchData(classId, temp);
                    },
                    selected: status.contains("PROCESSING"),
                    elevation: 1,
                    backgroundColor: Colors.grey,
                    selectedColor: AppTheme.colors.orangeBtn,
                    labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                  ),
                ),
                ChoiceChip(
                  label: Text(
                    "Graded",
                    style: TextStyle(color: AppTheme.colors.lightOrange),
                  ),
                  onSelected: (value) {
                    List<String> temp = status;
                    if (!value)
                      temp.remove("GRADED");
                    else
                      temp.add("GRADED");
                    setState(() {
                      status = temp;
                    });
                    fetchData(classId, status);
                  },
                  selected: status.contains("GRADED"),
                  elevation: 1,
                  backgroundColor: Colors.grey,
                  selectedColor: AppTheme.colors.orangeBtn,
                  labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                ),
                // Container(
                //     width: 90.0,
                //     child: LabeledCheckbox(
                //       label: "New",
                //       value: status.contains("SUBMITTED"),
                //       onChanged: (value) {
                //         List<String> temp = status;
                //         if (!value)
                //           temp.remove("SUBMITTED");
                //         else
                //           temp.add("SUBMITTED");
                //         setState(() {
                //           status = temp;
                //         });
                //         fetchData(classId, temp);
                //       },
                //       padding: const EdgeInsets.all(1.0),
                //     )),
                // Container(
                //     width: 130,
                //     child: LabeledCheckbox(
                //       label: "Processing",
                //       value: status.contains("PROCESSING"),
                //       onChanged: (value) {
                //         List<String> temp = status;
                //         if (!value)
                //           temp.remove("PROCESSING");
                //         else
                //           temp.add("PROCESSING");
                //         setState(() {
                //           status = temp;
                //         });
                //         fetchData(classId, temp);
                //       },
                //       padding: const EdgeInsets.all(1.0),
                //     )),
                // Container(
                //     width: 120,
                //     child: LabeledCheckbox(
                //       label: "Graded",
                //       value: status.contains("GRADED"),
                //       onChanged: (value) {
                //         List<String> temp = status;
                //         if (!value)
                //           temp.remove("GRADED");
                //         else
                //           temp.add("GRADED");
                //         setState(() {
                //           status = temp;
                //         });
                //         fetchData(classId, status);
                //       },
                //       padding: const EdgeInsets.all(1.0),
                //     )),
              ],
            ),
          ),
          _reportsController.obx((data) {
            if (_reportsController.teacherAssessmentRecord.value.list != null) {
              return Container(
                  child: AssessmentsRecordsDataGrid(
                      _reportsController.teacherAssessmentRecord.value.list!,
                      new AssessmentTeacherQueryDto(status: status, type: [])));
            } else {
              return Text('');
            }
          },
              onEmpty: Container(
                  child: AssessmentsRecordsDataGrid(<QuestionRecord>[],
                      new AssessmentTeacherQueryDto(status: status, type: []))),
              onLoading: Container())
        ]));
  }
}
